package com.leo.manage_system.controller;


import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import com.leo.manage_system.utils.JwtUtils;
import com.leo.manage_system.utils.PageUtil;
import com.leo.manage_system.utils.R;
import com.leo.manage_system.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 用户基础信息表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-11-01
 */
@RestController
@RequestMapping("/service_manage/picture")
@Api(tags = "图片管理")
@CrossOrigin
public class PictureController1 {
    String ip="1.117.57.89";
    JwtUtils jwtUtils = new JwtUtils();
//    ("root", "Leo13777863501")

    @ApiOperation(value = "遍历头像返回头像名字")
    @PostMapping("PicturePageQuery/{page}/{limit}")
    public R PicturePageQuery(
            @ApiParam(name = "token", value = "token", required = true)
            @RequestParam(value="token") String token,
            @ApiParam(name = "page", value = "当前页码", required = true)
                        @PathVariable Integer page,
                    @ApiParam(name = "limit", value = "每页记录数", required = true)
                        @PathVariable Integer limit){
        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {
            //        String path = filePath;
            String path = "/usr/local/server/uploadFiles/Img";		//要遍历的路径
            File file = new File(path);		//获取其file对象
            File[] fs = file.listFiles();	//遍历path下的文件和目录，放在File数组中
//            Map<int,String> map = new HashMap<>();
            Map<String, Object> map1 = new HashMap<>();
            List<Map> list = new ArrayList<>();
            for(File f:fs){					//遍历File[]数组
                if(!f.isDirectory()) {        //若非目录(即文件)，则打印
//                    System.out.println(f.getPath());
                    Map<String, Object> map = new HashMap<>();

                    map.put("url",StringUtil.sub(f.getPath(),17,null,1));
                    map.put("pictureName",StringUtil.sub(f.getPath(),34,null,1));
                    list.add(map);
                }
            }

            PageUtil pageUtil = new PageUtil();
            List<Map> list1 = pageUtil.startPage(list,page,limit);
//            map1.put("item",list1);

            Integer pages=pageUtil.getPageCount();
            Integer total=pageUtil.getCount();

            map1.put("current",page);
            map1.put("pages",pages);
            map1.put("size",limit);
            map1.put("total",total);
            map1.put("items",list1);

            return R.ok().data(map1);
        }


//        boolean code = deleteServerFile(pathName);
//        if(!code) {
//            return R.error().message("删除失败");
//        }
//        return R.ok().message("删除成功");
    }


    @ApiOperation(value = "根据头像名删除头像")
    @DeleteMapping("deletePicture")
    public R deletePicture(
            @ApiParam(name = "token", value = "token", required = true)
            @RequestParam(value="token") String token,
            @ApiParam(name = "pathName", value = "图片名字", required = true)
            @RequestParam String pathName){
        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {
            boolean code = deleteServerFile(pathName);
            if(!code) {
                return R.error().message("删除失败");
            }
            return R.ok().message("删除成功");
        }

    }

    public static boolean deleteServerFile(String pathName){
        File file = new File(File.separator +"usr" + File.separator + "local" + File.separator + "server" + File.separator + "uploadFiles"+ File.separator + "Img"+ File.separator, pathName);
        boolean delete_flag = false;
        if (!file.exists()) {
            return delete_flag;
        }
        try{
            delete_flag = file.delete();
        }catch (Exception e){
            e.printStackTrace();
        }
        return delete_flag;
    }






}

